#include "ISAPISettings.h"
#include "regkey.h"
#include "FileLog.h"

#define MAX_ARRAY 24*1024
#define REGISTRY_ENTRY "SOFTWARE\\Iiis6SqlInjection"

CISAPISettings::CISAPISettings(void)
: isLogEnabled(false), 
runOnce(0),
level(None)
{
	RegKey reg(HKEY_LOCAL_MACHINE);

	if(ERROR_SUCCESS != reg.Open(REGISTRY_ENTRY, KEY_READ))
	{
			reg.Close();
			//properties.MessageBox(L"Unable to create registry key.\nPlease log in as Administrator.", L"Error");
			return;
	}
	TCHAR buffLogFolder[260] = { 0 };
	TCHAR exclusions[MAX_ARRAY] = {0};
	DWORD logDW = 0;
	DWORD logSeverity = 0;
	DWORD exclusionDW = 0;
	RegValue regValue;
	regValue = reg["LogFolder"];
	if(regValue.Type != REG_NONE )
	{
		_tcsncpy(buffLogFolder, reg["LogFolder"].QueryValue(), 260);
	}
	logFolder.assign(buffLogFolder);
	regValue = reg["Log"];
	if(regValue.Type != REG_NONE )
	{
		_memccpy(&logDW, reg["Log"].QueryValue(), sizeof(DWORD), sizeof(DWORD)); 
	}
	isLogEnabled = (logDW != 0);
	regValue = reg["LogSeverity"];
	if(regValue.Type != REG_NONE )
	{
		_memccpy(&logSeverity, reg["LogSeverity"].QueryValue(), sizeof(DWORD), sizeof(DWORD)); 
	}
	logSeverity = min(logSeverity, 4); 
	level = (LOGLEVEL)logSeverity;

	regValue = reg["Exclusion"];
	if(regValue.Type != REG_NONE )
	{
		_memccpy(&exclusionDW, reg["Exclusion"].QueryValue(), sizeof(DWORD), sizeof(DWORD)); 
	}
	if(exclusionDW == 0)
		return;  // ignore exclusion list
	regValue = reg["ExclusionFiles"];
	DWORD size = min(regValue.Size(), MAX_ARRAY);
	if(regValue.Type != REG_NONE )
	{
		_memccpy(&exclusions, reg["ExclusionFiles"].QueryValue(), size, size); 
	}


	LPCTSTR pointer = (LPCTSTR)&exclusions[0];
	while(*pointer != '\0')
	{
		exclusionFiles.push_back(pointer);
		pointer+=lstrlen(pointer)+1;

	}

}

CISAPISettings::~CISAPISettings(void)
{
}


ISAPISETTINGS CISAPISettings::GetBulkSettings(void)
{
	ISAPISETTINGS settings;
	settings.IsLogEnabled = isLogEnabled;
	settings.level = level;
	settings.LogFolder = logFolder;
	return settings;
}

// Get path only (e.g. \path\ -> \path\)
string CISAPISettings::GetFolder(const string& Path)
{
  size_t found;
  found=Path.find_last_of("/\\");
  return Path.substr(0,found);
}


// Get file extension only (e.g. \path\test.aspx -> aspx)
string CISAPISettings::GetExtension(const string& Path)
{
  size_t found;
  found=Path.find_last_of(".");
  return Path.substr(found+1);
}


// Get only file name (e.g. \path\test.aspx -> test.aspx)
string CISAPISettings::GetFilename(const string& Path)
{
  size_t found;
  found=Path.find_last_of("/\\");
  return Path.substr(found+1);
}


// Verify if file is in the exclusion list
bool CISAPISettings::IgnoreThisFile(string Filename)
{
	vector<string>::iterator i = exclusionFiles.begin();
	
	while(i != exclusionFiles.end())
	{
		string FilenameOnly = GetFilename(Filename);
		if(Upper(FilenameOnly) == Upper(*i))
			return true;
		if((string)(*i).substr(0,2) == "*.")
		{
			if(Upper(GetExtension(FilenameOnly)) == Upper(GetExtension(*i)))
				return true;
		}

		i++;
	}
	return false;
}

string CISAPISettings::Upper(string& MyString)
{
   for(int i=0;i<MyString.length();i++)
   {
      MyString[i] = toupper(MyString[i]);
   }
   return MyString;//return the converted string
}
